#include "common.h"

void* _cdecl operator new(size_t allocSize)
{
	KdPrint(("Enter global operator new\n"));
	KdPrint(("Allocated %d size\n", allocSize));
	return ExAllocatePool(PagedPool, allocSize);
}

void* _cdecl operator new(size_t allocSize, POOL_TYPE PoolType)
{
	KdPrint(("Enter global operator new with pool_type\n"));
	KdPrint(("Allocated %d size\n", allocSize));
	return ExAllocatePool(PoolType, allocSize);
}

void _cdecl operator delete(void* pointer)
{
	KdPrint(("Enter global operator delete\n"));
	ExFreePool(pointer);
	KdPrint(("Leave global operator delete\n"));

	return;
}

NTSTATUS CreateDeviece(
					   IN PDRIVER_OBJECT driverObject, 
					   IN PUNICODE_STRING deviceName,
					   OUT PDEVICE_OBJECT& deviceObject,
					   IN PUNICODE_STRING symbolName /* = NULL */, 
					   IN size_t deviceExtensionSize /* = 0 */
					   )

{
	NTSTATUS	status = STATUS_SUCCESS;

	deviceObject = NULL;
	__try
	{
		status = IoCreateDevice(
			driverObject,
			deviceExtensionSize,
			deviceName,
			FILE_DEVICE_UNKNOWN,
			0,
			FALSE,
			&deviceObject);
		if(!NT_SUCCESS(status))
		{
			return status;
		}

		deviceObject->Flags |= DO_BUFFERED_IO;

		if(symbolName != NULL)
		{
			status = IoCreateSymbolicLink(symbolName, deviceName);
			if(!NT_SUCCESS(status))
			{
				return status;
			}
		}
		
	}
	__finally
	{
		if(!NT_SUCCESS(status))
		{
			if(symbolName != NULL)
				IoDeleteSymbolicLink(symbolName);
			if(deviceObject != NULL)
				IoDeleteDevice(deviceObject);
		}
	}

	return status;


}